﻿import mx.events.EventDispatcher;

class com.vitaly.FlickrCommunicator {

	function dispatchEvent() {
	}

	function addEventListener() {
	}

	function removeEventListener() {
	}

	var api_key : String;
	//var base_url = "http://174.133.9.68/flashwidget/get_album.php";
	var base_url = "flashwidget/get_album.php";
	var my_xml : XML;

	function FlickrCommunicator(mykey : String) {
		mx.events.EventDispatcher.initialize(this);
		api_key = mykey;
		my_xml = new XML();
		
		
		my_xml.ignoreWhite = true;
		my_xml["parent"] = this;
		
		my_xml.onLoad = function(success : Boolean) {
			var img_dir : String;
			var results_array = new Array();
			trace("Did it load? " + this);
			if (success) {
				_level0.img_dir = this.firstChild.firstChild.attributes.dir;
				_level0.author = this.firstChild.attributes.name;
				_level0.authorID = this.firstChild.attributes.ID;
				_level0.album_txt = this.firstChild.attributes.albom;
				
				for (var current_node : XMLNode = this.firstChild.firstChild.firstChild;current_node != null; current_node = current_node.nextSibling) {
					results_array.push(current_node.attributes);
				}
				//trace("Resulting in " + results_array);
				this["parent"].dispatchEvent({type:"searchEvent", success:true, results:results_array, dir:img_dir});
			} else {
				this["parent"].dispatchEvent({type:"searchEvent", success:false});
			}
		};
		trace("Construction complete");
	}

	function getBestPhotos(per_page : Number) {
		trace("WIDTH: "+Stage.width);
		var url_string = base_url+"?albumID="+_level0.albom+"&scale=noscale&width="+_level0.StageWidth+"&height="+_level0.StageHeight+"&"+random(3636);
		
		//url_string += "method=flickr.interestingness.getList";
		//url_string += "&api_key="+api_key;
		
		//if (per_page) {
		//	url_string += "&per_page="+per_page;
		//	}
		
		//trace("Loading from..." + url_string);

		my_xml.load(url_string);
	}
}
